package com.ias.agdis_r.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.bean.task.FLG10TaskBean;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.util.S;
import com.ias.agdis_r.util.flight.AirlineCompanyUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by guoxr on 2015/12/2.
 */
public class QJTaskSearchExpandableAdapter extends BaseExpandableListAdapter {
    private List<String> groupList = new ArrayList<String>();
    private List<List<FLG10TaskBean>> childList = new ArrayList<List<FLG10TaskBean>>();
    private List<FLG10TaskBean> mList = new ArrayList<FLG10TaskBean>();
    private Context context;
    private boolean isTem = true;

    public QJTaskSearchExpandableAdapter(Context context, List<String> groupList,
                                         List<List<FLG10TaskBean>> childList) {
        this.groupList = groupList;
        this.childList = childList;
        this.context = context;
        sort();
    }

    static class GroupViewHolder {
        TextView txtTEM, txtNUM;
        ImageView ivCompany;
    }

    static class ChildViewHolder {
        TextView txtHangbanhao, txtJihao, txtJiwei, txtJiXing,
                txtZhiXing, txtPnm, txtShijian;
        ImageView ivadid;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        GroupViewHolder holder;
        if (convertView == null) {
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.item_qjtasksearch_temadapter, null);
            holder = new GroupViewHolder();
            holder.txtTEM = (TextView) convertView.findViewById(R.id.txt_TEM);
            holder.txtNUM = (TextView) convertView.findViewById(R.id.txt_NUM);
            holder.ivCompany = (ImageView) convertView.findViewById(R.id.iv_company);
            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }
        if (null != groupList && null != childList.get(groupPosition)) {
            if (!isTem) {
                holder.ivCompany.setBackgroundResource(AirlineCompanyUtil.getArilineCompanyIconResourceId(groupList.get(groupPosition)));
            } else {
                holder.ivCompany.setBackgroundResource(R.drawable.user_online);
            }
            holder.txtTEM.setText(groupList.get(groupPosition));
            int i = 0;
            for (FLG10TaskBean task : childList.get(groupPosition)) {
                if (!MessageModel.TCXX.equals(task.getPid())
                        && !MessageModel.TRJT.equals(task.getPid())
                        && !MessageModel.TEND.equals(task.getPid())
                        && !MessageModel.T_QJHS_YQ.equals(task.getPid())
                        && !MessageModel.TTRN.equals(task.getPid())) {
                    i++;
                }
            }
            holder.txtNUM.setText(i + "/"
                    + getChildrenCount(groupPosition));
            if (0 == i) {
                holder.txtNUM.setTextColor(Color.rgb(255, 0, 0));
                holder.txtTEM.setTextColor(Color.rgb(255, 0, 0));
            } else {
                holder.txtNUM.setTextColor(Color.rgb(0, 0, 0));
                holder.txtTEM.setTextColor(Color.rgb(0, 0, 0));
            }
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder holder;
        if (convertView == null) {
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.item_qjtasksearch_adapter, null);
            holder = new ChildViewHolder();
            holder.ivadid = (ImageView) convertView.findViewById(R.id.item_qjmonitor_adidImage);
            holder.txtHangbanhao = (TextView) convertView.findViewById(R.id.item_qjmonitor_fln);
            holder.txtJiwei = (TextView) convertView.findViewById(R.id.item_qjmonitor_psn);
            holder.txtShijian = (TextView) convertView.findViewById(R.id.item_qjmonitor_time);
            holder.txtZhiXing = (TextView) convertView.findViewById(R.id.item_qjmonitor_tem);
            holder.txtJiXing = (TextView) convertView.findViewById(R.id.item_qjmonitor_acn);
            holder.txtPnm = (TextView) convertView.findViewById(R.id.item_qjmonitor_state);
            holder.txtJihao = (TextView) convertView.findViewById(R.id.item_qjmonitor_regnumber);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        if (null != childList) {
            mList = childList.get(groupPosition);
            if (null != mList) {
                holder.txtHangbanhao.setText(mList.get(childPosition).getFln());
                if ("A".equals(mList.get(childPosition).getAod())) {
                    holder.ivadid.setImageResource(R.drawable.adida_red);
                } else {
                    holder.ivadid.setImageResource(R.drawable.adidd);
                }
                holder.txtZhiXing.setText(mList.get(childPosition).getTem());
                FLG10TaskBean FLG10Task = mList.get(childPosition);
                String pnm = FLG10Task.getTst();
                if (pnm.equals(context.getString(R.string.paifa))) {
                    holder.txtPnm.setText(context.getString(R.string.unaccept));
                    holder.txtPnm.setBackgroundResource(R.drawable.textview_background2);
                } else if (MessageModel.TCXX.equals(FLG10Task.getPid())
                        || MessageModel.TRJT.equals(FLG10Task.getPid())
                        || MessageModel.TEND.equals(FLG10Task.getPid())
                        || MessageModel.T_QJHS_YQ.equals(FLG10Task.getPid())
                        || MessageModel.TTRN.equals(FLG10Task.getPid())) {
                    holder.txtPnm.setText(pnm);
                    holder.txtPnm.setBackgroundResource(R.drawable.textview_background3);
                } else {
                    holder.txtPnm.setText(pnm);
                    holder.txtPnm.setBackgroundResource(R.drawable.textview_background);
                }
                holder.txtShijian.setText(context.getString(R.string.time)+S.FormatTime2(mList.get(childPosition).getTime()));
                holder.txtJiwei.setText(mList.get(childPosition).getPsn() != null ? context.getString(R.string.VFS_2) + mList.get(childPosition).getPsn() : context.getString(R.string.VFS_2));
                holder.txtJiXing.setText(mList.get(childPosition).getAcn() != null ? context.getString(R.string.VFS_1) + mList.get(childPosition).getAcn() : context.getString(R.string.VFS_1));
                holder.txtJihao.setText(mList.get(childPosition).getReg() != null ? context.getString(R.string.VFS_3) + mList.get(childPosition).getReg() : context.getString(R.string.VFS_3));
            }
        }
        return convertView;
    }

    public void setType(boolean type) {
        isTem = type;
    }

    @Override
    public FLG10TaskBean getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

    @Override
    public String getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public void notifyDataSetChanged() {
        sort();
        super.notifyDataSetChanged();
    }

    private void sort()
    {
        for(List child:childList){
            //通过比较器实现比较排序
            Collections.sort(child, new Comparator<FLG10TaskBean>() {
                public int compare(FLG10TaskBean task1, FLG10TaskBean task2) {
                    String pidx1 = task1.getPidx();
                    String pidx2 = task2.getPidx();
                    if (null == pidx1){
                        pidx1 = "";
                    }
                    if (null == pidx2){
                        pidx2 = "";
                    }
                    int pidxCom = pidx1.compareTo(pidx2);
                    if (pidxCom == 0) {
                        String time1 = task1.getTime();
                        String time2 = task2.getTime();
                        if (null == time1){
                            time1 = "";
                        }
                        if (null == time2){
                            time2 = "";
                        }
                        int timeCom = time2.compareTo(time1);
                        return timeCom;
                    }

                    return pidxCom;
                }
            });
        }
    }
}
